package com.zenchn.schedule.task;

import com.zenchn.model.TSysLoginAccessRecordExample;
import com.zenchn.service.LoginAccessRecordService;
import com.zenchn.utils.SpringBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 *  访问记录 清除三个月以上记录 定时任务
 * @author panbingqi
 * @date 2021-06-01
 */
@Component
public class LoginAccessRecordTask implements Runnable {
    private Logger mLogger = LoggerFactory.getLogger(LoginAccessRecordTask.class);

    @Override
    public void run() {
        try {
            mLogger.debug("访问记录删除任务启动");
            LoginAccessRecordService recordService = SpringBeanUtil.getBean(LoginAccessRecordService.class);

            TSysLoginAccessRecordExample example = new TSysLoginAccessRecordExample();
            example.createCriteria().andAccessTimeLessThan(LocalDateTime.now().minusDays(90));

            recordService.deleteByExample(example);
            mLogger.debug("超过三个月的访问记录删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            mLogger.error("访问记录删除任务错误：{}", e.getMessage());
        }
    }
}
